const app = getApp();
const updateValue = require('./../../../utils/updateValue')
const qiniu = require('./../../../utils/qiniuSdk')
const time = require('./../../../utils/getTime')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isdefined: 1,
        id: undefined,
        detail: {},
        date: '',
        testNum: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let id = undefined;
        if (options.id) {
            id = options.id
        }
        this.setData({
            isdefined: Number(options.isdefined),
            id: id
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        if (this.data.id) {
            this.getInfo();
        } else {
            this.setData({
                ['detail.diagnosisDate']: time(),
                date: time()
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    // 初始化
    getInfo() {
        let val = {
            medicalRecordId: this.data.id
        }
        app.$http.get(app.$api.caseDetail, val).then(({
            data
        }) => {
            this.setData({
                detail: data,
                date: data.diagnosisDate,
                testNum: data.symptomDescription ? data.symptomDescription.length : 0
            }, () => {
                wx.setNavigationBarTitle({
                    title: `${this.data.detail.diagnosisDate} 病例`
                })
            })
        })
    },
    // 双向数据绑定
    updateValue(e) {
        let type = e.currentTarget.dataset.type
        updateValue.apply(this, [e, 'detail']);
        if (type === 'symptomDescription') {
            this.setData({
                testNum: this.data.detail.symptomDescription.length
            })
        }
    },
    // 上传图片
    upload() {
        app.$http.get(app.$api.qiniuUpload).then(({
            data
        }) => {
            let token = data.token;
            wx.chooseImage({
                count: 1,
                sourceType: ['album', 'camera'],
                success: ({
                    tempFilePaths
                }) => {
                    qiniu.upload(tempFilePaths[0], ({
                        imageURL
                    }) => {
                        let val = {
                            imagePath: imageURL
                        }
                        let imgList = this.data.detail.mrImageInfos || [];
                        imgList.push(val);
                        this.setData({
                            ['detail.mrImageInfos']: imgList
                        })
                    }, err => {
                        console.log(err)
                    }, {
                        region: 'ECN',
                        domain: 'https://images.bempty.cn',
                        uptoken: token,
                        uptokenURL: token,
                        uptokenFunc: () => token
                    })
                }
            })
        })
    },
    // 时间选择
    bindDateChange(e) {
        this.setData({
            ['detail.diagnosisDate']: e.detail.value,
            date: e.detail.value
        })
    },
    // 预览图片
    previewImage(e) {
        let info = e.currentTarget.dataset;
        let imgList = []
        info.imglist.forEach(item => {
            imgList.push(item.imagePath)
        })
        wx.previewImage({
            current: info.img,
            urls: imgList
        })
    },
    // 删除图片
    delImg(e) {
        let i = e.currentTarget.dataset.index;
        let imgList = this.data.detail.mrImageInfos;
        imgList.splice(i, 1);
        this.setData({
            ['detail.mrImageInfos']: imgList
        })
    },
    // 保存
    sub() {
        let header = {
            'Content-Type': 'application/json'
        }
        app.$http.post(app.$api.saveCase, this.data.detail, header).then(({
            msg
        }) => {
            app.$tap.showToast(msg);
            wx.navigateBack({
                delta: 1
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})